/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.domain.LabSwipeCardRecord;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.dto.CabinetDto;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.LabSwipeCardRecordRepository;
import me.zhengjie.modules.biz.service.LabSwipeCardRecordService;
import me.zhengjie.modules.biz.service.dto.LabSwipeCardRecordDto;
import me.zhengjie.modules.biz.service.dto.LabSwipeCardRecordQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.LabSwipeCardRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class LabSwipeCardRecordServiceImpl implements LabSwipeCardRecordService {

    private final LabSwipeCardRecordRepository labSwipeCardRecordRepository;
    private final LabSwipeCardRecordMapper labSwipeCardRecordMapper;
    private final LabRepository labRepository;
    private final UserRepository userRepository;

    @Override
    public Map<String,Object> queryAll(LabSwipeCardRecordQueryCriteria criteria, Pageable pageable){
        Page<LabSwipeCardRecord> page = labSwipeCardRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<LabSwipeCardRecordDto> dto = page.map(labSwipeCardRecordMapper::toDto);
        List<LabSwipeCardRecordDto> resultDto = dto.getContent();
        for (LabSwipeCardRecordDto labSwipeCardRecordDto:resultDto) {
            Lab lab = labRepository.findById(labSwipeCardRecordDto.getLabId()).orElseGet(Lab::new);
            labSwipeCardRecordDto.setLabName(lab.getName());
            User user = userRepository.findById(Long.valueOf(labSwipeCardRecordDto.getUserId())).orElseGet(User::new);
            labSwipeCardRecordDto.setUserName(user.getUsername());
        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<LabSwipeCardRecordDto> queryAll(LabSwipeCardRecordQueryCriteria criteria){
        List<LabSwipeCardRecord> labSwipeCardRecordList = labSwipeCardRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<LabSwipeCardRecordDto> labSwipeCardRecordDtoList = labSwipeCardRecordMapper.toDto(labSwipeCardRecordList);
        for (LabSwipeCardRecordDto labSwipeCardRecordDto : labSwipeCardRecordDtoList) {
            Lab lab = labRepository.findById(labSwipeCardRecordDto.getLabId()).orElseGet(Lab::new);
            labSwipeCardRecordDto.setLabName(lab.getName());
            User user = userRepository.findById(Long.valueOf(labSwipeCardRecordDto.getUserId())).orElseGet(User::new);
            labSwipeCardRecordDto.setUserName(user.getUsername());
        }
        return labSwipeCardRecordDtoList;
    }

    @Override
    @Transactional
    public LabSwipeCardRecordDto findById(Integer id) {
        LabSwipeCardRecord labSwipeCardRecord = labSwipeCardRecordRepository.findById(id).orElseGet(LabSwipeCardRecord::new);
        ValidationUtil.isNull(labSwipeCardRecord.getId(),"LabSwipeCardRecord","id",id);
        return labSwipeCardRecordMapper.toDto(labSwipeCardRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LabSwipeCardRecordDto create(LabSwipeCardRecord resources) {
        return labSwipeCardRecordMapper.toDto(labSwipeCardRecordRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LabSwipeCardRecord resources) {
        LabSwipeCardRecord labSwipeCardRecord = labSwipeCardRecordRepository.findById(resources.getId()).orElseGet(LabSwipeCardRecord::new);
        ValidationUtil.isNull( labSwipeCardRecord.getId(),"LabSwipeCardRecord","id",resources.getId());
        labSwipeCardRecord.copy(resources);
        labSwipeCardRecordRepository.save(labSwipeCardRecord);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            labSwipeCardRecordRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LabSwipeCardRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LabSwipeCardRecordDto labSwipeCardRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("进出人员", labSwipeCardRecord.getUserName());
            String type = "";
            switch (labSwipeCardRecord.getType()) {
                case 1:
                    type = "进入";
                    break;
                case 2:
                    type = "出去";
                    break;
            }
            map.put("类型", type);
            map.put("实验室", labSwipeCardRecord.getLabName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
